package com.atguigu.user.service.impl;

import com.atguigu.user.mapper.AddressMapper;
import com.atguigu.user.service.AddressService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.pojo.Address;

import java.util.List;

/**
 * ClassName:AddressServiceImpl
 * Package:com.atguigu.user.service.impl
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/21 16:53
 * @Version 1.0
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public R list(Integer userId) {
//        封装查询参数
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<Address>();
        addressQueryWrapper.eq("user_id",userId);
        List<Address> addressesList = addressMapper.selectList(addressQueryWrapper);

        log.info("AddressServiceImpl.list业务结束，结果：{}",R.ok("查询成功",addressesList));
        return R.ok("查询成功",addressesList);
    }

    @Override
    public R save(Address address) {
//        插入数据
        int rows = addressMapper.insert(address);

        if (rows==0){
            log.info("AddressServiceImpl.save业务结束，结果：{}","插入地址失败");
            return R.fail("插入地址失败");
        }

        log.info("AddressServiceImpl.save业务结束，结果：{}",list((address.getUserId())));
//        插入成功的话，就调用list方法查询用户信息并返回
        return list(address.getUserId());


    }

    @Override
    public R remove(Integer id) {
        int rows = addressMapper.deleteById(id);
        if (rows==0){
            log.info("AddressServiceImpl.remove业务结束，结果：{}","删除失败");
            return R.fail("删除地址数据失败");
        }
        log.info("AddressServiceImpl.remove业务结束，结果：{}","删除成功");
        return R.ok("删除成功");
    }
}
